//% color="#ff9f06" iconWidth=50 iconHeight=40
namespace RFID_Module{
    
    //% block="Initialize RFID module [VER]" blockType="command"  
    //% VER.shadow="dropdown" VER.options="VER"
    export function RFID_init(parameter: any, block: any) { 
        let ver = parameter.VER.code;

        if (ver == "V1"){
            Generator.addInclude('RFID1', '#include <DFRobot_PN7151.h>');
            Generator.addObject(`RFID2`, `DFRobot_PN7150_I2C`, `RFID;`);
            Generator.addSetup(`RFID.begin`, `
while (!(RFID.begin(NXPNCI_MODE_RW))) {
  Serial.println("init fail!");
}
Serial.println("successful connection.");
            `);
        }else{
            Generator.addInclude('RFID1', '#include <DFRobot_FM17550.h>');
            Generator.addObject(`RFID2`, `DFRobot_FM17550`, `RFID;`);
            Generator.addSetup(`RFID.begin`, `
while (!(RFID.begin())) {
  Serial.println("init fail!");
}
Serial.println("successful connection.");
            `);
        }

        
    }

    //% block="Write [DATA] to byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="command" 
    //% DATA.shadow="range" DATA.params.min=0 DATA.params.max=255 DATA.defl=200
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_write(parameter: any, block: any) { 
        let data=parameter.DATA.code;
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.writeData(${block_num}, ${byte_num}, ${data});`);

    }

    //% block="Read all data from block [BLOCK_NUM] of the RFID module(string)" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_read_all(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;

        Generator.addCode(`RFID.readData(${block_num})`);

    }

    //% block="Read byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    export function RFID_read(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.readData(${block_num}, ${byte_num})`);

    }



    //% block="Read the UID of the tag card" blockType="reporter" 
    export function RFID_read_UID(parameter: any, block: any) { 

        Generator.addCode(`RFID.readUid()`);

    }


    //% block="Detected card. UID:[UID]?" blockType="boolean" 
    //% UID.shadow="string" UID.defl="acee50e3"
    export function RFID_SCAN_UID(parameter: any, block: any) { 
        let uid=parameter.UID.code;

        Generator.addCode(`RFID.scan(${uid})`);

    }


    //% block="Detected card?" blockType="boolean" 
    export function RFID_SCAN(parameter: any, block: any) { 

        Generator.addCode(`RFID.scan()`);

    }

}



